package com.xjczjj.config.web;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.exception.NotLoginException;
import com.xjczjj.model.common.Result;
import com.xjczjj.model.common.ResultCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {Exception.class})
    public Result handleException(Exception e) {
        if (e instanceof NotLoginException) {
            SaHolder.getResponse().setStatus(ResultCode.SUCCESS);
            return Result.error().code(ResultCode.NO_LOGIN).message(e.getMessage());
        }

        return Result.error().code(ResultCode.SERVER_ERROR).message(e.getMessage());
    }
}
